package com.itgone.view.activity;

import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import com.andview.refreshview.utils.LogUtils;
import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.itgone.R;
import com.itgone.common.Const;
import com.itgone.presenter.ILoginPresenter;
import com.itgone.presenter.impl.LoginPresenterImpl;
import com.itgone.util.AppUtil;
import com.itgone.util.SPUtil;
import com.itgone.view.ILoginView;

import org.jsoup.helper.StringUtil;

public class LoginActivity extends BaseActivity implements ILoginView, View.OnLongClickListener {

    private AutoCompleteTextView mActvUsername;
    private AutoCompleteTextView mActvPassword;
    private ILoginPresenter mLoginPresenter;
    private Button mLoginButton, mRegisterButton;

    @Override
    int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
   protected void initView() {
        AppUtil.init(this);

        setTitle("登陆");
        mActvUsername = findViewById(R.id.actv_username);
        mActvPassword = findViewById(R.id.actv_password);
        mLoginButton = findViewById(R.id.login_button);
        mRegisterButton = findViewById(R.id.do_register_button);
    }

    @Override
    protected void initData() {
        mLoginPresenter = new LoginPresenterImpl(this);
        mLoginButton.setOnClickListener(this);
        mRegisterButton.setOnClickListener(this);
        mLoginButton.setOnLongClickListener(this);

        Object username = SPUtil.get(Const.KEY_USERNAME, "");
        Object password = SPUtil.get(Const.KEY_PASSWORD, "");

        if (!"".equals(username) && !"".equals(password)) {
            mActvUsername.setText(username.toString());
            mActvPassword.setText(password.toString());
            mLoginPresenter.doLogin();
        }
    }

    @Override
    public void showDialog(String msg) {
        ToastUtils.showToast(this, msg);
    }

    @Override
    public void setProgress(boolean visible) {

    }

    @Override
    public void cleanInput() {
        mActvPassword.setText("");
        mActvUsername.setText("");
    }

    @Override
    public void setPwdError(String msg) {
        mActvPassword.setError(msg);
    }

    @Override
    public void setUnError(String msg) {
        mActvUsername.setError(msg);
    }

    @Override
    public String getUsername() {
        return mActvUsername.getText().toString();
    }

    @Override
    public String getPassword() {
        return mActvPassword.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                mLoginPresenter.doLogin();
                break;
            case R.id.do_register_button:
                AppUtil.openActivity(RegisterActivity.class);
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
//        AppUtil.openActivity(MainActivity.class);
        mActvUsername.setText("earlman");
        mActvPassword.setText("1234567");
        return true;
    }
}
